package com.dongdong.service.impl;

import cn.hutool.core.util.StrUtil;
import com.dongdong.mapper.TbGoodsDescMapper;
import com.dongdong.mapper.TbGoodsMapper;
import com.dongdong.model.TbGoods;
import com.dongdong.model.TbGoodsDesc;

import com.dongdong.model.TbGoodsExample;
import com.dongdong.result.PageBean;
import com.dongdong.result.PageResult;
import com.dongdong.service.GoodsDescService;
import com.dongdong.service.GoodsService;
import com.dongdong.service.ItemService;
import com.dongdong.vo.GoodsDescVO;
import com.dongdong.vo.GoodsVO;
import com.dongdong.vo.ItemVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private TbGoodsMapper goodsMapper;

    @Autowired
    private TbGoodsDescMapper goodsDescMapper;
    @Autowired
    private GoodsDescService goodsDescService;
    @Autowired
    private ItemService itemService;

 //增加
    @Override
    public void insert(GoodsVO goodsVO) {

        TbGoods tbGoods = new TbGoods();
        BeanUtils.copyProperties(goodsVO, tbGoods);
        tbGoods.setAuditStatus("0");
        goodsMapper.insertbygoodsid(tbGoods);//增加主表   //增加成功后返回主键id

        Long id = tbGoods.getId();

        //增加从表
        GoodsDescVO goodDescVO = goodsVO.getGoodDescVO();
        TbGoodsDesc tbGoodsDesc = new TbGoodsDesc();
        BeanUtils.copyProperties(goodDescVO, tbGoodsDesc);
        tbGoodsDesc.setGoodsId(id);
        goodsDescMapper.insertSelective(tbGoodsDesc);  //增加从表



    }
//分页
    @Override
    public PageResult findAllPage(PageBean<TbGoods> pageBean) {
        TbGoodsExample example = new TbGoodsExample();
        TbGoodsExample.Criteria criteria = example.createCriteria();
        if(!StrUtil.isEmptyIfStr(pageBean.getBean().getGoodsName())){
            criteria.andGoodsNameLike("%"+pageBean.getBean().getGoodsName()+"%");
        }
        if(!StrUtil.isEmptyIfStr(pageBean.getBean().getAuditStatus())){
            criteria.andAuditStatusEqualTo(pageBean.getBean().getAuditStatus());
        }
        //分页部分
        PageHelper.startPage(pageBean.getPageNumber(),pageBean.getPageSize());
        List<TbGoods> sellers = goodsMapper.selectByExample(example);
        PageInfo<TbGoods> info = new PageInfo<>(sellers);
        //转换
        List<GoodsVO> vos = info.getList().stream().map((x) -> {
            GoodsVO vo = new GoodsVO();
            BeanUtils.copyProperties(x, vo);
            return vo;
        }).collect(Collectors.toList());
        return new PageResult<>(info.getPageNum(),info.getPageSize(),info.getTotal(),vos);
    }

    @Override
    public TbGoods selectById(String id) {
        return goodsMapper.selectByPrimaryKey(Long.valueOf(id));

    }

//审核
    @Override
    public void updateStatus(String auditStatus, Long id) {
        TbGoods tbGoods = goodsMapper.selectByPrimaryKey(id);
        tbGoods.setAuditStatus(auditStatus);
        goodsMapper.updateByPrimaryKeySelective(tbGoods);
    }

    @Override
    public GoodsVO selectGoodsAndDesc(Long goodId) {
        TbGoods tbGoods = goodsMapper.selectByPrimaryKey(goodId);
        //转换
        GoodsVO goodVO= new GoodsVO();
        BeanUtils.copyProperties(tbGoods, goodVO);
        // 根据goodid查询
        GoodsDescVO goodDescVO = goodsDescService.selectDesc(goodId);
        goodVO.setGoodDescVO(goodDescVO);

        //查询当前goodid对应的所有sku 信息
        List<ItemVO> itemVOS = itemService.findItemByGoodId(goodId);goodVO.setItemVOS(itemVOS);

         return goodVO;

    }













}
